package com.cskaoyan.service;

import com.cskaoyan.bean.MarketAddress;
import com.cskaoyan.bean.MarketAddressExample;
import com.cskaoyan.mapper.MarketAddressMapper;
import com.cskaoyan.mapper.MarketRegionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * 收货地址管理
 *
 * @author Fyy
 * @since 2023/01/01 15:37
 */

@Service
public class WxAddressServiceImpl implements WxAddressService {

    @Autowired
    MarketAddressMapper addressMapper;

    /**
     * 查询收货地址列表
     *
     * @param id
     * @return
     */
    @Override
    public List<MarketAddress> list(Integer id) {
        //新增条件
        MarketAddressExample example = new MarketAddressExample();
        MarketAddressExample.Criteria criteria = example.createCriteria();
        //判空
        if (ObjectUtils.isEmpty(id)) {
            //不为空添加条件
            criteria.andUserIdEqualTo(id);
        }
        //只查询逻辑未删除的数据
        criteria.andDeletedEqualTo(false);
        List<MarketAddress> list = addressMapper.selectByExample(example);
        return list;
    }

    /**
     * 新增收货地址
     *
     * @param marketAddress
     * @return
     */

    @Override
    public Integer save(MarketAddress marketAddress) {
        //获取新增收货地址的id
        if (marketAddress.getIsDefault() == true) {
            MarketAddressExample example = new MarketAddressExample();
            example.createCriteria().andIsDefaultEqualTo(true);
            MarketAddress marketAddress1 = new MarketAddress();
            marketAddress1.setIsDefault(false);
            addressMapper.updateByExampleSelective(marketAddress1, example);
        }
        addressMapper.insertSelective(marketAddress);
        Integer id = marketAddress.getId();
        return id;
    }


    /**
     * 收货地址详情
     *
     * @param id
     * @return
     */
    @Override
    public MarketAddress detail(Integer id) {
        MarketAddress marketAddress = addressMapper.selectByPrimaryKey(id);
        return marketAddress;
    }

    /**
     * 逻辑删除收货地址
     *
     * @param marketAddress
     */
    @Override
    public void delete(MarketAddress marketAddress) {
        addressMapper.updateByPrimaryKeySelective(marketAddress);
    }


}
